import { Page } from '@/base/Page';
import { DeviceUse } from '@/base/model/DeviceUse';
import { deleteDeviceUse, getDeviceUseList, deleteDeviceFault } from '@/base/api/DeviceUseApi';
import { isEmpty } from '@/base/util/Strings';

class Form {
  createBegin = '';
  createEnd = '';
  date = [];
}

export class MassageLogBloc {
  form = new Form();
  paged: Page<DeviceUse> = new Page<DeviceUse>([], 0, 1, 15, 0, []);
  loading = false;

  load(pageIndex: number, pageSize: number, affiliateId: number) {
    if (!isEmpty(this.form.date) && !isEmpty(String(this.form.date[0]))) {
      // @ts-ignore
      this.form.createBegin = this.form.date[0].getTime() || '';
      // @ts-ignore
      this.form.createEnd = this.form.date[1].getTime() + 24 * 60 * 60 * 1000 || '';
    } else {
      this.form.createBegin = '';
      this.form.createEnd = '';
    }
    this.loading = true;
    return getDeviceUseList({
      pageIndex,
      pageSize,
      sortDirection: 'DESC',
      affiliateId,
      createBegin: this.form.createBegin,
      createEnd: this.form.createEnd
    }).then((res) => {
      this.paged = res.data;
    }).finally(() => {
      this.loading = false;
    });
  }

  removeItem(dateItem: DeviceUse) {
    this.loading = true;
    return deleteDeviceUse(dateItem.id)
      .finally(() => {
        this.loading = false;
      });
  }
  removeFaultItem(dateItem: DeviceUse) {
    this.loading = true;
    return deleteDeviceFault(dateItem.id)
      .finally(() => {
        this.loading = false;
      });
  }
}
